package com.letian.record.service.impl;

import java.util.List;
import com.letian.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.letian.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.letian.record.domain.RecordOrderItem;
import com.letian.record.mapper.RecordOrderMapper;
import com.letian.record.domain.RecordOrder;
import com.letian.record.service.IRecordOrderService;

/**
 * 记录订单Service业务层处理
 * 
 * @author bmz
 * @date 2023-07-20
 */
@Service
public class RecordOrderServiceImpl implements IRecordOrderService 
{
    @Autowired
    private RecordOrderMapper recordOrderMapper;

    /**
     * 查询记录订单
     * 
     * @param orderId 记录订单主键
     * @return 记录订单
     */
    @Override
    public RecordOrder selectRecordOrderByOrderId(Long orderId)
    {
        return recordOrderMapper.selectRecordOrderByOrderId(orderId);
    }

    /**
     * 查询记录订单列表
     * 
     * @param recordOrder 记录订单
     * @return 记录订单
     */
    @Override
    public List<RecordOrder> selectRecordOrderList(RecordOrder recordOrder)
    {
        return recordOrderMapper.selectRecordOrderList(recordOrder);
    }

    /**
     * 新增记录订单
     * 
     * @param recordOrder 记录订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRecordOrder(RecordOrder recordOrder)
    {
        recordOrder.setCreateTime(DateUtils.getNowDate());
        int rows = recordOrderMapper.insertRecordOrder(recordOrder);
        insertRecordOrderItem(recordOrder);
        return rows;
    }

    /**
     * 修改记录订单
     * 
     * @param recordOrder 记录订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRecordOrder(RecordOrder recordOrder)
    {
        recordOrder.setUpdateTime(DateUtils.getNowDate());
        recordOrderMapper.deleteRecordOrderItemByOrderId(recordOrder.getOrderId());
        insertRecordOrderItem(recordOrder);
        return recordOrderMapper.updateRecordOrder(recordOrder);
    }

    /**
     * 批量删除记录订单
     * 
     * @param orderIds 需要删除的记录订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRecordOrderByOrderIds(Long[] orderIds)
    {
        recordOrderMapper.deleteRecordOrderItemByOrderIds(orderIds);
        return recordOrderMapper.deleteRecordOrderByOrderIds(orderIds);
    }

    /**
     * 删除记录订单信息
     * 
     * @param orderId 记录订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRecordOrderByOrderId(Long orderId)
    {
        recordOrderMapper.deleteRecordOrderItemByOrderId(orderId);
        return recordOrderMapper.deleteRecordOrderByOrderId(orderId);
    }

    /**
     * 新增记录订单项信息
     * 
     * @param recordOrder 记录订单对象
     */
    public void insertRecordOrderItem(RecordOrder recordOrder)
    {
        List<RecordOrderItem> recordOrderItemList = recordOrder.getRecordOrderItemList();
        Long orderId = recordOrder.getOrderId();
        if (StringUtils.isNotNull(recordOrderItemList))
        {
            List<RecordOrderItem> list = new ArrayList<RecordOrderItem>();
            for (RecordOrderItem recordOrderItem : recordOrderItemList)
            {
                recordOrderItem.setOrderId(orderId);
                list.add(recordOrderItem);
            }
            if (list.size() > 0)
            {
                recordOrderMapper.batchRecordOrderItem(list);
            }
        }
    }
}
